package com.example.projectcode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.projectcode.mapper.ChapterMapper;
import com.example.projectcode.mapper.ChoiceMapper;
import com.example.projectcode.mapper.SubjectMapper;
import com.example.projectcode.mapper.TestPointMapper;
import com.example.projectcode.model.Chapter;
import com.example.projectcode.model.Choice;
import com.example.projectcode.model.Subject;
import com.example.projectcode.model.Testpoint;
import com.example.projectcode.service.ChoiceService;
import com.example.projectcode.service.SubjectService;
import com.example.projectcode.vo.ChoiceVo;
import com.example.projectcode.vo.UpdateChoiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChoiceServiceImpl extends ServiceImpl<ChoiceMapper, Choice> implements ChoiceService {

    @Autowired
    private TestPointMapper testPointMapper;

    @Autowired
    private ChoiceMapper choiceMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private ChapterMapper chapterMapper;



    @Override
    public List<Choice> selectAllChoice(Integer testPointid, String questionCategory) {
//        QueryWrapper<Choice> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("Testpointid",testPointid).eq("C_type",questionCategory);
//       List<Choice> choiceList = baseMapper.selectList(queryWrapper);
        List<Choice> choiceList = new ArrayList<>();
        choiceList = choiceMapper.selectAllChoices(testPointid,questionCategory);
        return choiceList;
    }

    @Override
    public Choice selectChoice(Integer testPointid, String questionCategory, Integer order) {


        return choiceMapper.selectChoice(testPointid,questionCategory,order);
    }

    @Override
    public int countChoice(Integer TestPointid, String questionCategory) {

            return (int) choiceMapper.selectAllChoices(TestPointid,questionCategory).stream().count();
    }

    @Override
    public Choice selectChoiceByCId(Integer cId) {
        return baseMapper.selectOne(new QueryWrapper<Choice>().eq("C_id",cId));
    }

    @Override
    public int insertChoice(ChoiceVo choiceVo) {

        //根据考点名查询考点Id
        Testpoint testpoint = new Testpoint();
        QueryWrapper<Testpoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Testpointname",choiceVo.getTestPointName());
        testpoint = testPointMapper.selectOne(queryWrapper);

        int testPointId = testpoint.getTestpointid();
        int chapterId = Integer.parseInt(testpoint.getChapid());

        //根据章节id查询章节
        Chapter chapter = new Chapter();
        QueryWrapper<Chapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("Chapid",chapterId);
        chapter  = chapterMapper.selectOne(queryWrapper1);


        int SubjectId = chapter.getSubid();

        Choice choice = new Choice();
        choice.setSubid(SubjectId);
        choice.setChapid(chapterId);
        choice.setTestpointid(testPointId);
        choice.setCStem(choiceVo.getCStem());
        choice.setOptionA(choiceVo.getOptionA());
        choice.setOptionB(choiceVo.getOptionB());
        choice.setOptionC(choiceVo.getOptionC());
        choice.setOptionD(choiceVo.getOptionD());
        choice.setCAnswer(choiceVo.getCAnswer());
        choice.setCType(choiceVo.getCType());
        choice.setCGraphlink(choiceVo.getCGraphlink());
        choice.setCVideolink(choiceVo.getCVideolink());

      return   choiceMapper.insert(choice);

    }

    @Override
    public int deleteCHoiceById(Integer choiceId) {
        return baseMapper.delete(new QueryWrapper<Choice>().eq("C_id",choiceId));
    }

    @Override
    public int updateChoiceById(UpdateChoiceVo updateChoiceVo) {
        int result = choiceMapper.updateChoiceById(updateChoiceVo);
        return result;
    }

    @Override
    public List<Choice> selectCHoiceByTestPointId(Integer testPointId) {
        return baseMapper.selectList(new QueryWrapper<Choice>().eq("Testpointid",testPointId));
    }


}
